package com.szqhyxj.youxiangju.biz.equipmentActivation.equipmentCardslot;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.szqhyxj.youxiangju.BaseActivity;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.equipmentActivation.activationrecord.ActivationRecordActivity;
import com.szqhyxj.youxiangju.biz.equipmentActivation.buySlots.BuySlotsActivity;
import com.szqhyxj.youxiangju.biz.merchantHome.deviceinfo.DeviceInfo;
import com.szqhyxj.youxiangju.sys.components.NavigationBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindColor;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by leung on 2018/11/3.
 */

public class EquipmentCardSlotActivity extends BaseActivity implements EquipmentCardSlotContract.View, NavigationBar.NavigationBarListener {


    EquipmentCardSlotPresenter presenter;

    @BindView(R.id.navigationBar)
    NavigationBar navigationBar;
    /*@BindView(R.id.address_prov_city)
    TextView addressProvCity;
    @BindView(R.id.deviceNo)
    TextView deviceNo;
    @BindView(R.id.deviceicon)
    ImageView deviceicon;
    @BindView(R.id.portNum)
    TextView portNum;
    @BindView(R.id.cardslotList)
    ListView cardslotList;
    @BindView(R.id.activatedevice)
    TextView activatedevice;
    @BindView(R.id.activationrecord)
    TextView activationrecord;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    @BindView(R.id.aepnType)
    TextView aepnType;
    @BindView(R.id.battery)
    TextView battery;
    @BindView(R.id.update)
    TextView update;
    @BindView(R.id.version)
    TextView version;*/
    @BindColor(R.color.textcolor66)
    int textcolor66;
    @BindView(R.id.deviceicon)
    ImageView deviceicon;
    @BindView(R.id.aepnType)
    TextView aepnType;
    @BindView(R.id.version)
    TextView version;
    @BindView(R.id.update)
    TextView update;
    @BindView(R.id.deviceType)
    TextView deviceType;
    @BindView(R.id.deviceNo)
    TextView deviceNo;
    @BindView(R.id.address_prov_city)
    TextView addressProvCity;
    @BindView(R.id.battery)
    TextView battery;
    @BindView(R.id.slotNumber)
    TextView slotNumber;
    private CardSlotAdapter adapter;
    List<SlotBean> listSlots;

    public static void start(Activity activity) {
        Intent intent = new Intent();
        intent.setClass(activity, EquipmentCardSlotActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_information);
        ButterKnife.bind(this);
        presenter = new EquipmentCardSlotPresenter(this);
        //  presenter.getDeviceInfo();

        navigationBar.setNavigationBarListener(this);
        listSlots = new ArrayList<>();
        adapter = new CardSlotAdapter(this, listSlots);
        //cardslotList.setAdapter(adapter);
        presenter.setdeviceinfo();
        initViews();
        presenter.getSlotList(DeviceInfo.Holder.getInstance().getDevNo());
    }

    @Override
    public void deviceinit() {
        if (DeviceInfo.Holder.getInstance().getOnlineState()!=null&&DeviceInfo.Holder.getInstance().getOnlineState().equals("1")){
            //设备在线
            aepnType.setText(String.format("%s", DeviceInfo.Holder.getInstance().getAepnType()));
            if (DeviceInfo.Holder.getInstance().getBattery() == null || DeviceInfo.Holder.getInstance().getBattery().equals("")) {
                battery.setText("--");
            } else {
                battery.setText( DeviceInfo.Holder.getInstance().getBattery());
            }
            if (DeviceInfo.Holder.getInstance().getUpdateDate() == null || DeviceInfo.Holder.getInstance().getUpdateDate().equals("")) {
                update.setText("--");
            } else {
                update.setText( DeviceInfo.Holder.getInstance().getUpdateDate());
            }
            deviceNo.setText(String.format("%s", DeviceInfo.Holder.getInstance().getDevNo()));
            addressProvCity.setText(String.format("%s", DeviceInfo.Holder.getInstance().getAreaInfo()));
            if (DeviceInfo.Holder.getInstance().getLocalVersion() == null || DeviceInfo.Holder.getInstance().getLocalVersion().equals("")) {
                version.setText("--");
            } else {
                version.setText(String.format("%s", DeviceInfo.Holder.getInstance().getLocalVersion()));
            }


        }else{
            //设备离线
            aepnType.setText("已离线");
            aepnType.setTextColor(textcolor66);
            if (DeviceInfo.Holder.getInstance().getBattery() == null || DeviceInfo.Holder.getInstance().getBattery().equals("")) {
                battery.setText( "--");
            } else {
                battery.setText( DeviceInfo.Holder.getInstance().getBattery());
            }
            if (DeviceInfo.Holder.getInstance().getUpdateDate() == null || DeviceInfo.Holder.getInstance().getUpdateDate().equals("")) {
                update.setText("--");
            } else {
                update.setText(DeviceInfo.Holder.getInstance().getUpdateDate());
            }
            deviceNo.setText(String.format("%s", DeviceInfo.Holder.getInstance().getDevNo()));
            addressProvCity.setText(String.format("%s", DeviceInfo.Holder.getInstance().getAreaInfo()));
            if (DeviceInfo.Holder.getInstance().getLocalVersion() == null || DeviceInfo.Holder.getInstance().getLocalVersion().equals("")) {
                version.setText("--");
            } else {
                version.setText(String.format("%s", DeviceInfo.Holder.getInstance().getLocalVersion()));
            }
        }



       /* if (DeviceInfo.Holder.getInstance().getOnlineState() == null || DeviceInfo.Holder.getInstance().getOnlineState().equals("")) {
            aepnType.setText("已离线");
            aepnType.setTextColor(textcolor66);
            if (DeviceInfo.Holder.getInstance().getBattery() == null || DeviceInfo.Holder.getInstance().getBattery().equals("")) {
                battery.setText(String.format("剩余电量：%s", "--"));
            } else {
                battery.setText(String.format("剩余电量：%s", DeviceInfo.Holder.getInstance().getBattery()));
            }
            if (DeviceInfo.Holder.getInstance().getUpdateDate() == null || DeviceInfo.Holder.getInstance().getUpdateDate().equals("")) {
                update.setText(String.format("上次更新：%s", "--"));
            } else {
                update.setText(String.format("上次更新：%s", DeviceInfo.Holder.getInstance().getUpdateDate()));
            }
            deviceNo.setText(String.format("%s", DeviceInfo.Holder.getInstance().getDevNo()));
            addressProvCity.setText(String.format("%s", DeviceInfo.Holder.getInstance().getAreaInfo()));
            if (DeviceInfo.Holder.getInstance().getLocalVersion() == null || DeviceInfo.Holder.getInstance().getLocalVersion().equals("")) {
                version.setVisibility(View.GONE);
            } else {
                version.setText(String.format("%s", DeviceInfo.Holder.getInstance().getLocalVersion()));
            }
        } else {
            if (DeviceInfo.Holder.getInstance().getOnlineState().equals("1")) {
                aepnType.setText(String.format("%s", DeviceInfo.Holder.getInstance().getAepnType()));
                if (DeviceInfo.Holder.getInstance().getBattery() == null || DeviceInfo.Holder.getInstance().getBattery().equals("")) {
                    battery.setText(String.format("剩余电量：%s", "--"));
                } else {
                    battery.setText(String.format("剩余电量：%s", DeviceInfo.Holder.getInstance().getBattery()));
                }
                if (DeviceInfo.Holder.getInstance().getUpdateDate() == null || DeviceInfo.Holder.getInstance().getUpdateDate().equals("")) {
                    update.setText(String.format("上次更新：%s", "--"));
                } else {
                    update.setText(String.format("上次更新：%s", DeviceInfo.Holder.getInstance().getUpdateDate()));
                }
                deviceNo.setText(String.format("%s", DeviceInfo.Holder.getInstance().getDevNo()));
                addressProvCity.setText(String.format("%s", DeviceInfo.Holder.getInstance().getAreaInfo()));
                if (DeviceInfo.Holder.getInstance().getLocalVersion() == null || DeviceInfo.Holder.getInstance().getLocalVersion().equals("")) {
                    version.setVisibility(View.GONE);
                } else {
                    version.setText(String.format("%s", DeviceInfo.Holder.getInstance().getLocalVersion()));
                }

            } else {
                aepnType.setText("已离线");
                aepnType.setTextColor(textcolor66);
                if (DeviceInfo.Holder.getInstance().getBattery() == null || DeviceInfo.Holder.getInstance().getBattery().equals("")) {
                    battery.setText(String.format("剩余电量：%s", "--"));
                } else {
                    battery.setText(String.format("剩余电量：%s", DeviceInfo.Holder.getInstance().getBattery()));
                }
                if (DeviceInfo.Holder.getInstance().getUpdateDate() == null || DeviceInfo.Holder.getInstance().getUpdateDate().equals("")) {
                    update.setText(String.format("上次更新：%s", "--"));
                } else {
                    update.setText(String.format("上次更新：%s", DeviceInfo.Holder.getInstance().getUpdateDate()));
                }
                deviceNo.setText(String.format("%s", DeviceInfo.Holder.getInstance().getDevNo()));
                addressProvCity.setText(String.format("%s", DeviceInfo.Holder.getInstance().getAreaInfo()));
                if (DeviceInfo.Holder.getInstance().getLocalVersion() == null || DeviceInfo.Holder.getInstance().getLocalVersion().equals("")) {
                    version.setVisibility(View.GONE);
                } else {
                    version.setText(String.format("%s", DeviceInfo.Holder.getInstance().getLocalVersion()));
                }
            }
        }*/


    }

    private void initViews() {
       /* swipeLayout.setColorSchemeResources(R.color.colorPrimaryDark);
        swipeLayout.setProgressViewOffset(true, 0, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources().getDisplayMetrics()));
        swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                presenter.getSlotList(DeviceInfo.Holder.getInstance().getDevNo());
                showRefresh(true);
            }
        });*/

    }

    @Override
    public void showRefresh(boolean isRefresh) {
//        if (swipeLayout == null) {
//            return;
//        }
//        swipeLayout.setRefreshing(isRefresh);
    }


    @Override
    protected void onResume() {
        super.onResume();
        presenter.getSlotList(DeviceInfo.Holder.getInstance().getDevNo());
    }

    @Override
    public void showAddress(final String areaInfo) {
        // addressProvCity.setText(areaInfo);
    }

    @Override
    public void showDeviceNo(String devNo) {
        //   deviceNo.setText(devNo);
    }

    @Override
    public void showDeviceicon(String type) {
        if (type.equals("Z90")) {
            deviceicon.setImageDrawable(getResources().getDrawable(R.mipmap.pos_16));
            deviceType.setText("YXJ-001");
            slotNumber.setText("17");
        }
        if (type.equals("TC1010")) {
            deviceicon.setImageDrawable(getResources().getDrawable(R.mipmap.pos_04));
            deviceType.setText("YXJ-002");
            slotNumber.setText("4");
        }
    }

    private String slots;

    @Override
    public void showSlotList(List<SlotBean> list) {
      /*  listSlots.clear();
        if (swipeLayout.isRefreshing()) {
            swipeLayout.setRefreshing(false);
        }
        listSlots.addAll(list);
        adapter.notifyDataSetChanged();*/
    }


    @Override
    public void showActivatedevice() {
     //   activatedevice.setVisibility(View.VISIBLE);
    }

    @Override
    public void enshowActivatedevice() {
    //    activatedevice.setVisibility(View.GONE);
    }

    @Override
    public void showToast(String s) {
        Toast.makeText(this, s, Toast.LENGTH_LONG).show();
    }

    @Override
    public void showList() {

    }

    @Override
    public void navigationLeft() {

        finish();
    }

    @Override
    public void navigationRight() {

    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            // MerchantContainerActivity.start(this);
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }


}
